package monsters
{
	import org.flixel.*;
	import com.greensock.TweenMax;
	import com.greensock.easing.Sine;
	import com.greensock.easing.Linear;
	import states.PlayState;

	public class Lefty extends FlxSprite
	{
		[Embed(source = '../../assets/gasparzinho.png')] private var gasparzinhoPNG:Class;
		[Embed(source = '../../assets/smiley.png')] private var smileyPNG:Class;
		
		public var isDying:Boolean = false;
		
		public function Lefty(x:int, y:int)
		{
			super(x * 32, y * 32);
			
			if (PlayState.levelInt == 0)
			{
				loadGraphic(gasparzinhoPNG, true, true, 32, 32);
				
			}else if(PlayState.levelInt == 1)
			{
				loadGraphic(smileyPNG, true, true, 19, 27);
				
			}else (PlayState.levelInt == 2)
			{
				loadGraphic(gasparzinhoPNG, true, true, 32, 32);
			}
			
			facing = FlxObject.RIGHT;
			
			addAnimation("walkNormal", [0, 1], 6, true);
			addAnimation("walkGelo", [4, 5, 6, 7], 6, true);
			addAnimation("walkFogo", [8, 9, 10, 11], 6, true);
			play("walkNormal");
			
			acceleration.y = 50;
			velocity.x = 30;
		}
		
		public function change(controle:int):void
		{			
			if (controle <= 0) {
				init();		
				
			}else if (controle > 0 && controle <= 1) {
				initFire();
				
			}else if (controle > 1 && controle <= 2) {
				initGelo();
			}
		}
		
		private function init():void
		{	
			play("walkNormal");
			
			acceleration.y = 100;
			if (facing == FlxObject.RIGHT)
			{				
				velocity.x = 60;
			}
			else
			{
				
				velocity.x = -60;
			}
		}
		
		private function initFire():void
		{			
			play("walkGelo");
			
			acceleration.y = 200;
			if (facing == FlxObject.RIGHT)
			{				
				velocity.x = 120;
			}
			else
			{
				
				velocity.x = -120;
			}
		}
		
		private function initGelo():void
		{
			play("walkFogo");
			
			acceleration.y = 50;
			if (facing == FlxObject.RIGHT)
			{				
				velocity.x = 30;
			}
			else
			{
				
				velocity.x = -30;
			}
		}
		
		override public function kill():void
		{
			FlxG.play(catty2SFX, 0.5, false, true);
			
			isDying = true;
			
			frame = 1;
			
			velocity.x = 0;
			velocity.y = 0;
			
			angle = 180;
			
			TweenMax.to(this, 1.5, { bezier: [ { x:"64", y:"-64" }, { x:"80", y:"200" } ], onComplete: removeSprite } );
			this.alive = false;
		}
		
		private function removeSprite():void
		{
			exists = false;
		}
		
		override public function update():void
		{
			super.update();
			
			//	Check the tiles on the left / right of it
			
			var tx:int = int(x / 32);
			var ty:int = int(y / 32);
			
			if (facing == FlxObject.LEFT)
			{
				//	31 is the Collide Index of our Tilemap (which sadly isn't exposed in Flixel 2.5, so is hard-coded here. Not ideal I appreciate)
				if (Registry.map.getTile(tx - 1, ty) >= 31)
				{
					turnAround();
					return;
				}
			}
			else
			{
				//	31 is the Collide Index of our Tilemap (which sadly isn't exposed in Flixel 2.5, so is hard-coded here. Not ideal I appreciate)
				if (Registry.map.getTile(tx + 1, ty) >= 31)
				{
					turnAround();
					return;
				}
			}
			
			//	Check the tiles below it
			
			if (isTouching(FlxObject.FLOOR) == false && isDying == false)
			{
				turnAround();
			}
		}
		
		private function turnAround():void
		{
			if (facing == FlxObject.RIGHT)
			{
				facing = FlxObject.LEFT;
				
				velocity.x = -1 * velocity.x;
			}
			else
			{
				facing = FlxObject.RIGHT;
				
				velocity.x = -1 * velocity.x;
			}
		}
		
	}

}